package cn.b.sky.mes.andon.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.b.sky.mes.andon.dao.WcAndonConfigDao;
import cn.b.sky.mes.andon.dao.WuAndonConfigDao;
import cn.b.sky.mes.andon.enums.AndonConfigType;
import cn.b.sky.mes.andon.model.AndonConfig;
import cn.b.sky.mes.andon.service.AndonConfigService;
import cn.b.sky.common.model.ImeCommon;
import cn.b.sky.user.SecurityContextUtil;

@Service("andonConfigService")
public class AndonConfigServiceImpl implements AndonConfigService {

  @Resource
  WcAndonConfigDao wcAndonConfigDao;

  @Resource
  WuAndonConfigDao wuAndonConfigDao;

  @Override
  public List<AndonConfig> getAndonConfigByCode(String type, String siteCode, String code) {
    List<AndonConfig> andonConfigs = null;
    if (AndonConfigType.WORKCENTER.getValue().equals(type)) {
      andonConfigs = wcAndonConfigDao.getAndonConfigByWorkCenterCode(siteCode, code);
    } else if (AndonConfigType.WORKUNIT.getValue().equals(type)) {
      andonConfigs = wuAndonConfigDao.getAndonConfigByworkUnitCode(siteCode, code);
    }
    return andonConfigs;
  }

  @Override
  public void saveWcAndonConfig(String siteCode, List<AndonConfig> configs, String workCenterCode) {
    boolean state;
    List<AndonConfig> oldandonConfigs = wcAndonConfigDao.getAndonConfigByWorkCenterCode(siteCode, workCenterCode);
    if (null != configs) {
      //新增、修改
      for (AndonConfig newAndonConfig : configs) {
        state = true;
        for (AndonConfig oldandonConfig : oldandonConfigs) {
          //存在，修改
          if (newAndonConfig.getAndonCode().equals(oldandonConfig.getAndonCode())) {
            newAndonConfig.setWorkCenterCode(workCenterCode);
            newAndonConfig.setSiteCode(siteCode);
            setModifyInfo(newAndonConfig);
            wcAndonConfigDao.updateWcAndonConfig(newAndonConfig);
            state = false;
          }
        }
        if (state) {
          //新增
          newAndonConfig.setWorkCenterCode(workCenterCode);
          newAndonConfig.setSiteCode(siteCode);
          setCreateInfo(newAndonConfig);
          wcAndonConfigDao.insertWcAndonConfig(newAndonConfig);
          state = true;
        }
      }
      //删除单个
      for (AndonConfig oldandonConfig : oldandonConfigs) {
        state = true;
        for (AndonConfig newAndonConfig : configs) {
          if (oldandonConfig.getAndonCode().equals(newAndonConfig.getAndonCode())) {
            state = false;
          }
        }
        if (state) {
          wcAndonConfigDao.daleteWcAndonConfig(siteCode, workCenterCode, oldandonConfig.getAndonCode());
          state = true;
        }
      }
    } else {
      for (AndonConfig oldandonConfig : oldandonConfigs) {
        wcAndonConfigDao.daleteWcAndonConfig(siteCode, workCenterCode, oldandonConfig.getAndonCode());
      }
    }
  }

  @Override
  public void saveWuAndonConfig(String siteCode, List<AndonConfig> configs, String workUnitCode) {
    boolean state;
    List<AndonConfig> oldandonConfigs = wuAndonConfigDao.getAndonConfigByworkUnitCode(siteCode, workUnitCode);
    if (null != configs) {
      //新增、修改
      for (AndonConfig newAndonConfig : configs) {
        state = true;
        for (AndonConfig oldandonConfig : oldandonConfigs) {
          //存在，修改
          if (newAndonConfig.getAndonCode().equals(oldandonConfig.getAndonCode())) {
            newAndonConfig.setWorkUnitCode(workUnitCode);
            newAndonConfig.setSiteCode(siteCode);
            setModifyInfo(newAndonConfig);
            wuAndonConfigDao.updateWuAndonConfig(newAndonConfig);
            state = false;
          }
        }
        if (state) {
          //新增
          newAndonConfig.setWorkUnitCode(workUnitCode);
          newAndonConfig.setSiteCode(siteCode);
          setCreateInfo(newAndonConfig);
          wuAndonConfigDao.insertWuAndonConfig(newAndonConfig);
          state = true;
        }
      }
      //删除
      for (AndonConfig oldandonConfig : oldandonConfigs) {
        state = true;
        for (AndonConfig newAndonConfig : configs) {
          if (oldandonConfig.getAndonCode().equals(newAndonConfig.getAndonCode())) {
            state = false;
          }
        }
        if (state) {
          wuAndonConfigDao.daleteWuAndonConfig(siteCode, workUnitCode, oldandonConfig.getAndonCode());
          state = true;
        }
      }
    } else {
      for (AndonConfig oldandonConfig : oldandonConfigs) {
        wuAndonConfigDao.daleteWuAndonConfig(siteCode, workUnitCode, oldandonConfig.getAndonCode());
      }
    }

  }

  public void setCreateInfo(ImeCommon imeCommon) {
    imeCommon.setCreateDateTime(new Date());
    imeCommon.setCreateUser(SecurityContextUtil.getCurrentUserName());
    this.setModifyInfo(imeCommon);
  }

  public void setModifyInfo(ImeCommon imeCommon) {
    imeCommon.setModifyDateTime(new Date());
    imeCommon.setModifyUser(SecurityContextUtil.getCurrentUserName());
  }
}
